package org.jeecg.modules.room.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.room.entity.RoomInfo;
import org.jeecg.modules.room.entity.RoomRepairPlan;
import org.jeecg.modules.room.mapper.RoomRepairPlanMapper;
import org.jeecg.modules.room.service.IRoomInfoService;
import org.jeecg.modules.room.service.IRoomRepairPlanService;
import org.jeecg.modules.room.vo.RoomRepairPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 维修计划表
 * @Author: jeecg-boot
 * @Date:   2024-01-02
 * @Version: V1.0
 */
@Service
public class RoomRepairPlanServiceImpl extends ServiceImpl<RoomRepairPlanMapper, RoomRepairPlan> implements IRoomRepairPlanService {

    @Autowired
    private IRoomInfoService roomInfoService;

    @Override
    public List<RoomRepairPlanVo> selectUnableDate(String roomCode) {
        return this.baseMapper.selectUnableDate(roomCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRepairPlan(RoomRepairPlan roomRepairPlan) {
        try {
            RoomRepairPlan repairPlan = this.baseMapper.selectById(roomRepairPlan.getId());
            if (repairPlan.getRepairStatus().equals("20")) {
                throw new JeecgBootException("当前维修计划已完成不可修改!");
            } else {
                this.baseMapper.deleteById(roomRepairPlan.getId());
                Page<RoomInfo> page = new Page<RoomInfo>(0, 10);
                IPage<RoomInfo> roomInfoList = roomInfoService.selectUsableRoomList(page, "", roomRepairPlan.getRoomCode(), "", "", "", roomRepairPlan.getBeginTime(), roomRepairPlan.getEndTime());
                if (roomInfoList == null || roomInfoList.getRecords().size() == 0) {
                    throw new JeecgBootException("当前房间已经占用，不能在该时间段进行维修，请核实！");
                }
                this.baseMapper.insert(roomRepairPlan);
            }
        }
        catch (Exception e){
            throw e;
        }
    }
}
